package io.renren.modules.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.shop.entity.CarEntity;
import io.renren.modules.shop.entity.CollectsEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.shop.entity.CollectEntity;
import io.renren.modules.shop.service.CollectService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author Yangqianyong
 * @email sunlightcs@gmail.com
 * @date 2021-11-17 15:35:50
 */
@RestController
@RequestMapping("shop/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("shop:collect:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = collectService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("shop:collect:info")
    public R info(@PathVariable("id") Long id){
		CollectEntity collect = collectService.getById(id);

        return R.ok().put("collect", collect);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("shop:collect:save")
    public R save(@RequestBody CollectEntity collect){
		collectService.save(collect);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("shop:collect:update")
    public R update(@RequestBody CollectEntity collect){
		collectService.updateById(collect);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("shop:collect:delete")
    public R delete(@RequestBody Long[] ids){
		collectService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //    获取用户收藏夹	getCollects :接收用户id
//            返回该用户的收藏的所有商品

    @RequestMapping("/detail")
    public R getCollects(@RequestParam Long id){
        List<CollectsEntity> list = collectService.getCollects(id);
        return R.ok().put("list",list);
    }

    //
//
//    添加到收藏夹	addCollect：接收 用户id 商品id     返回成功码
    @RequestMapping("/addCol")
    public R addCollect(@RequestParam Long uid,@RequestParam Long pid){
        QueryWrapper<CollectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CollectEntity::getProductId,pid);
        queryWrapper.lambda().eq(CollectEntity::getUserId,uid);
        CollectEntity collectEntity = collectService.getOne(queryWrapper);

        if(collectEntity!=null){
            return R.ok(1);
        }
        CollectEntity collectEntity1 = new CollectEntity();
        collectEntity1.setUserId(uid);
        collectEntity1.setProductId(pid);
        collectService.saveOrUpdate(collectEntity1);
        return R.ok();
    }

    //    删除收藏夹	deleteCollect：接收 用户id 商品id     返回成功码
    @RequestMapping("/deleteCol")
    public R deleteCollect(@RequestParam Long uid,@RequestParam Long pid){
        QueryWrapper<CollectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CollectEntity::getProductId,pid);
        queryWrapper.lambda().eq(CollectEntity::getUserId,uid);
        collectService.remove(queryWrapper);
        return R.ok();
    }
}
